const neo4j = require('neo4j-driver');

const driver = neo4j.driver('bolt://localhost:7687/', neo4j.auth.basic('neo4j', 'fy7777777'));
const session = driver.session();

const books = [
    { id: '1', name: 'Book 1', author: 'Author 1', abstract: 'Abstract 1', type: 'Type 1', label: 'Label 1', score: 8.5 },
    { id: '2', name: 'Book 2', author: 'Author 2', abstract: 'Abstract 2', type: 'Type 1', label: 'Label 2', score: 9.0 },
    { id: '3', name: 'Book 3', author: 'Author 3', abstract: 'Abstract 3', type: 'Type 2', label: 'Label 1', score: 7.5 },
    { id: '4', name: 'Book 4', author: 'Author 1', abstract: 'Abstract 4', type: 'Type 2', label: 'Label 2', score: 8.0 },
];

async function createBookNodes() {
    for (const book of books) {
        const result = await session.run(
            `CREATE (b:Book {id: $id, name: $name, author: $author, abstract: $abstract, type: $type, label: $label, score: $score}) RETURN b`,
            book
        );
        console.log(result.records[0].get('b').properties);
    }
}

async function createAuthorNodes() {
    const authors = Array.from(new Set(books.map((book) => book.author))); // 去重获取所有作者

    for (const author of authors) {
        const result = await session.run(`CREATE (a:Author {name: $name}) RETURN a`, { name: author });
        console.log(result.records[0].get('a').properties);
    }
}

async function createBookAuthorRelationships() {
    for (const book of books) {
        const result = await session.run(
            `MATCH (b:Book {id: $bookId}), (a:Author {name: $authorName}) CREATE (b)-[:WRITTEN_BY]->(a) RETURN b, a`,
            { bookId: book.id, authorName: book.author }
        );
        console.log(result.records[0].get('b').properties, result.records[0].get('a').properties);
    }
}

async function createTypeNodes() {
    const types = Array.from(new Set(books.map((book) => book.type))); // 去重获取所有类型

    for (const type of types) {
        const result = await session.run(`CREATE (t:Type {name: $name}) RETURN t`, { name: type });
        console.log(result.records[0].get('t').properties);
    }
}

async function createBookTypeRelationships() {
    for (const book of books) {
        const result = await session.run(
            `MATCH (b:Book {id: $bookId}), (t:Type {name: $typeName}) CREATE (b)-[:BELONGS_TO]->(t) RETURN b, t`,
            { bookId: book.id, typeName: book.type }
        );
        console.log(result.records[0].get('b').properties, result.records[0].get('t').properties);
    }
}

async function createLabelNodes() {
    const labels = Array.from(new Set(books.map((book) => book.label))); // 去重获取所有标签

    for (const label of labels) {
        const result = await session.run(`CREATE (l:Label {name: $name}) RETURN l`, { name: label });
        console.log(result.records[0].get('l').properties);
    }
}

async function createBookLabelRelationships() {
    for (const book of books) {
        const result = await session.run(
            `MATCH (b:Book {id: $bookId}), (l:Label {name: $labelName}) CREATE (b)-[:HAS_LABEL]->(l) RETURN b, l`,
            { bookId: book.id, labelName: book.label }
        );
        console.log(result.records[0].get('b').properties, result.records[0].get('l').properties);
    }
}

async function createScoreRelationships() {
    for (const book of books) {
        const result = await session.run(
            `MATCH (b:Book {id: $bookId}) SET b.score = $score RETURN b`,
            { bookId: book.id, score: book.score }
        );
        console.log(result.records[0].get('b').properties);
    }
}

async function createBookGraph() {
    try {
        await createBookNodes();
        await createAuthorNodes();
        await createBookAuthorRelationships();
        await createTypeNodes();
        await createBookTypeRelationships();
        await createLabelNodes();
        await createBookLabelRelationships();
        await createScoreRelationships();
    } catch (error) {
        console.error(error);
    } finally {
        await session.close();
        await driver.close();
    }
}

createBookGraph();

